package com.ys.controller.web.business;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.entity.business.TcwVatPaymentAccount;
import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.TsUserChange;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/business/tcwVatPaymentAccount")
public class WebTcwVatPaymentAccountController {
	

	@Autowired
	private CommonApiService<TcwVatPaymentAccount> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model){
		try {
			TcwVatPaymentAccount entity = new TcwVatPaymentAccount();
			TsUserChange entity1 = new TsUserChange();
			ThrOperateCompanyApply thrOperateCompanyApply =  new ThrOperateCompanyApply().setCuser(entity1.getUserId()).queryFirst();
			Map<String, Object> extraParam = new HashMap<>();
			//关联数据
			extraParam.put("operatorUserName", thrOperateCompanyApply.getCUserName());
			entity.setCode(commonApiService.getCommonNextUkey(MenuId.TCW_VatPaymentAccount, "Code"))
					.setStatus(1).setStatusSec(1);
			return commonApiService.toAdd(model,entity, MenuId.TCW_VatPaymentAccount, null, null);

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, Long id){
		if (id == null){
			return W.error(model, "id不能为空");
		}

		if (new TcwVatPaymentAccount().setId(id).count() == 0){
			return W.error(model, "该信息不存在!");
		}

		try {
			Map<String,Object> entity = new TcwVatPaymentAccount()
					.addField("B.Code AS countryCode")
					.addField("B.Name AS countryName")
					.addField("C.Symbol AS amazonShopExchangeRate")
					.addField("(D.UserId + '/' + D.UserName) AS _ys_nameLinked")
					.addLeftJoin("TB_Country B on B.Code=A.CountryCode")
					.addLeftJoin("TP_AmazonShopExchangeRate C on C.Id=A.AmazonShopExchangeRateld")
					.addLeftJoin("TS_UserChange D on D.Id=A.CreateChangeld")
					.setId(id)
					.queryFirstMap();
			Map<String, Object> extraParam = new HashMap<>();
			extraParam.put("amazonShopExchangeRate", entity.get("amazonShopExchangeRate"));
			extraParam.put("amazonShopExchangeRateld", entity.get("amazonShopExchangeRateld"));
			model.addAttribute("onProcess", 0);

			return commonApiService.toEdit(model, id, new TcwVatPaymentAccount(), MenuId.TCW_VatPaymentAccount, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据出错，请联系客服!");
		}
	}

}
